package com.ytkj.ygAssist.server;

import com.ytkj.ygAssist.server.util.HttpGetUtil;
import com.ytkj.ygAssist.tools.CacheData;
import com.ytkj.ygAssist.tools.MyLog;
import com.ytkj.ygAssist.tools.YungouDataTools;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;

public class BackgroundMonitorServer {
	private static BackgroundMonitorServer backgroundMonitor = null;
	private Thread backgroundMonitorServerThread = null;

	public static BackgroundMonitorServer getBackgroundMonitor() {
		if (backgroundMonitor == null) {
			backgroundMonitor = new BackgroundMonitorServer();
		}
		return backgroundMonitor;
	}

	/*
	 * 用户监听操作
	 */
	public void startServer() {
		new Thread(new Runnable() {
			public void run() {
				while (true) {
					try {
						Thread.sleep(3000);
						MyLog.outLog("后台检测线程大小", GetUserBuyServer.getStartThread());
						if (GetUserBuyServer.getStartThread().size() == 0) {
							boolean isAllSelect = true;
							ArrayList<String> goodsMap = CacheData.getGoodsTreeListCacheDate();
							for (int i = goodsMap.size() - 1; i >= 0; i--) {
								if (GetUserBuyServer.getStartThread().size() == 0) {
									String goodsId = goodsMap.get(i);
									boolean isSelect = selectGoodsInfo(goodsId);
									if (isSelect) {
										isAllSelect = false;
									}
									Thread.sleep(6 * 1000);
								}
							}
							if (isAllSelect) {
								Object[] objects = GetGoodsInfo.getStartRaffleAllList("0");
								List<Map<String, String>> dList = (List<Map<String, String>>) objects[1];
								for (int i = dList.size() - 1; i >= 0; i--) {
									Map<String, String> map = dList.get(i);
									int price = Integer.parseInt(map.get("codeQuantity"));
									String goodsID = YungouDataTools.getGoodsID(map.get("goodsSName"),
											map.get("codeType"));
									CacheData.setGoodsPriceCacheDate(goodsID, price);
									selectZeroize(goodsID, map.get("period"), price, true);
									Thread.sleep(2 * 60 * 1000);
								}
							}
						}
					} catch (Exception e) {
					}
				}
			}
		}).start();
	}

	/*
	 * 用户监听最近查询商品补零
	 */
	private boolean selectGoodsInfo(String goodsID) {
		boolean isHavaData = false;
//		String text[] = GetGoodsInfo.shopCartNew(CacheData.getGoodsInfoCacheDate(goodsID)[2],
//				HttpGetUtil.getHttpClient());
//		String text[] = GetGoodsInfo.shopCartNewByHtml(goodsID,
//				HttpGetUtil.getHttpClient());
		String text[] = GetGoodsInfo.getGoodsSalingByGoodsID(goodsID,
				HttpGetUtil.getHttpClient());
		try {
			int newestPeriod = Integer.parseInt(text[1]);
			for (int i = 1; i < 50 && i < newestPeriod; i++) {
				if (CacheData.getSelectCacheDate(goodsID, Integer.toString(newestPeriod - i)) == null) {
					isHavaData = true;
					break;
				}
			}
			CacheData.setGoodsPriceCacheDate(goodsID, Integer.parseInt(text[3]));
		} catch (Exception e) {
		}
		if (isHavaData) {
			SelectAssistPublishs.getYungouPublishs(goodsID, "0", "50");
			List<Map<String, String>> contentList = GetGoodsInfo.getBarcodeRaffListByGoodsID(goodsID, "50");
			if (contentList != null) {
				try {
					ConcurrentHashMap<String, String[]> selectCacheDateMap = CacheData.getSelectCacheDate(goodsID);
					for (Map<String, String> map : contentList) {
						String codePeriod = map.get("codePeriod");
						String[] SelectText = selectCacheDateMap.get(codePeriod);
						String codeRNO = map.get("codeRNO");
						if (SelectText != null) {
							if (!codeRNO.equals("") && !SelectText[2].equals(codeRNO)) {
								MyLog.outLog("计算结果错误了", goodsID, codePeriod, "原", SelectText[2], "正确", codeRNO);
								SelectAssistPublishs.deleteYungouPublish(goodsID, codePeriod);
								CacheData.removeSelectCacheDate(goodsID, codePeriod);
								SelectText = null;
							}
						}
						if (SelectText == null) {
							if (!map.get("codeState").equals("1")) {
								String codeID = map.get("codeID");
								if (GetUserBuyServer.getStartThread().size() == 0) {
									if (codeRNO.equals("")) {
										int price = Integer.parseInt(map.get("codeQuantity"));
										CacheData.setGoodsPriceCacheDate(goodsID, price);
										new GetUserBuyServer().GetGoodsPeriodInfo(goodsID, codePeriod, codeID, false);
										MyLog.outLog("用户监听正在后台查", goodsID, codePeriod);
										Thread.sleep(6000);
									} else {
										GetUserBuyServer getUserBuyServer = new GetUserBuyServer();
										getUserBuyServer.setBarcodernoInfo(codeRNO, map.get("userName"),
												map.get("userWeb"));
										getUserBuyServer.GetGoodsPeriodInfo(goodsID, codePeriod, codeID, false);
										MyLog.outLog("正在后台查", goodsID, codePeriod);
										Thread.sleep(6000);
									}
								}
							}
						}
					}
				} catch (Exception e) {
				}
			}
		}
		return isHavaData;
	}

	/*
	 * 监听商品 goodsPriceType 0小件，1大件
	 */
	public void startBackgroundMonitorServerThread(final int goodsPriceType) {
		if (backgroundMonitorServerThread == null) {
			backgroundMonitorServerThread = new Thread(new Runnable() {
				public void run() {
					String maxSeconds = "0";
					while (true) {
						try {
							Object[] objects = GetGoodsInfo.getStartRaffleAllList(maxSeconds);
							maxSeconds = (String) objects[0];
							List<Map<String, String>> dList = (List<Map<String, String>>) objects[1];
							for (int i = dList.size() - 1; i >= 0; i--) {
								final Map<String, String> map = dList.get(i);
								int price = Integer.parseInt(map.get("codeQuantity"));
								if ((price < 7000 && goodsPriceType == 0)
										|| (price >= 7000 && price <= 30000 && goodsPriceType == 1)) {
									String goodsID = YungouDataTools.getGoodsID(map.get("goodsSName"),
											map.get("codeType"));
									CacheData.setGoodsPriceCacheDate(goodsID, price);
									System.out.println("准备：" + goodsID + dList.get(i).get("goodsSName"));
									if (goodsID != null
											&& !SelectAssistPublishs.getYungouPublishs(goodsID, map.get("period"))) {
										new GetUserBuyServer().GetGoodsPeriodInfo(goodsID, map.get("period"),
												map.get("codeID"), true);
										Thread.sleep(5000);
									}
								}
								if (price >= 30000) {
									final String goodsID = YungouDataTools.getGoodsID(map.get("goodsSName"),
											map.get("codeType"));
									System.out.println("超级大件：" + goodsID + map.get("goodsSName"));
									if (goodsID != null
											&& !SelectAssistPublishs.getYungouPublishs(goodsID, map.get("period"))) {
										CacheData.setGoodsPriceCacheDate(goodsID, price);
										new Timer().schedule(new TimerTask() {
											@Override
											public void run() {
												// new
												// GetUserBuyServerBigGoods(foreknowInterface,
												// "0").GetGoodsPeriodInfo(
												// goodsID, map.get("period"),
												// map.get("codeID"));
												new GetUserBuyServer().GetGoodsPeriodInfo(goodsID, map.get("period"),
														map.get("codeID"), true);
											}
										}, 31000);
										Thread.sleep(2000);
									}
								}
							}
							Thread.sleep(1000);
						} catch (Exception e) {
						}
					}
				}
			});
			backgroundMonitorServerThread.start();
		}
	}

	/*
	 * 服务器监听所有商品
	 */
	public void startBackgroundMonitorAllGoodsServerThread() {
		if (backgroundMonitorServerThread == null) {
			backgroundMonitorServerThread = new Thread(new Runnable() {
				public void run() {
					String maxSeconds = "0";
					while (true) {
						try {
							Object[] objects = GetGoodsInfo.getStartRaffleAllList(maxSeconds);
							maxSeconds = (String) objects[0];
							List<Map<String, String>> dList = (List<Map<String, String>>) objects[1];
							for (int i = dList.size() - 1; i >= 0; i--) {
								final Map<String, String> map = dList.get(i);
								int price = Integer.parseInt(map.get("codeQuantity"));
								String goodsSName = map.get("goodsSName");
								final String period = map.get("period");
								final String goodsID = YungouDataTools.getGoodsID(goodsSName, map.get("codeType"));
								CacheData.setGoodsPriceCacheDate(goodsID, price);
								MyLog.outLog("监听查询准备：", goodsID, goodsSName);
								if (goodsID != null && !SelectAssistPublishs.getYungouPublishs(goodsID, period)) {
									if (price <= 7000) {
										new GetUserBuyServer().GetGoodsPeriodInfo(goodsID, period, map.get("codeID"),
												true);
										Thread.sleep(5000);
									} else {
										new Timer().schedule(new TimerTask() {
											@Override
											public void run() {
												new GetUserBuyServer().GetGoodsPeriodInfo(goodsID, period,
														map.get("codeID"), true);
											}
										}, 31000);
										Thread.sleep(2000);
									}
								}
								Thread.sleep(1000);
							}
						} catch (Exception e) {
						}
					}
				}
			});
			backgroundMonitorServerThread.start();
		}
	}

	/*
	 * 监听补零 goodsPriceType 0小件，1大件，超大件
	 */
	public void startBackgroundMonitorServerThreadZeroize(final int goodsPriceType) {
		if (backgroundMonitorServerThread == null) {
			backgroundMonitorServerThread = new Thread(new Runnable() {
				public void run() {
					while (true) {
						try {
							List<Map<String, String>> sList = GetGoodsInfo.getGoodsList();
							for (Map<String, String> map : sList) {
								int price = Integer.parseInt(map.get("codeQuantity"));
								if ((price < 4000 && goodsPriceType == 0)
										|| (price >= 4000 && price < 30000 && goodsPriceType == 1)
										|| (price >= 30000 && goodsPriceType == 2)) {
									String goodsID = map.get("goodsID");
									CacheData.setGoodsPriceCacheDate(goodsID, price);
									selectZeroize(goodsID, map.get("codePeriod"), price, false);
								}
							}
						} catch (Exception e) {
						}
					}
				}
			});
			backgroundMonitorServerThread.start();
		}

	}

	/*
	 * 补零操作
	 */
	private void selectZeroize(String goodsID, String codePeriod, int price, boolean isUserQuery) {
		ArrayList<Integer> periodArray = SelectAssistPublishs.getYungouPublishsNotData(goodsID, codePeriod);
		// System.out.println("准备在查：" + goodsID + ":" + codePeriod);
		try {
			if (isUserQuery || periodArray.size() > 1) {
				List<Map<String, String>> codeList = GetGoodsInfo.getBarcodeRaffListByGoodsID(goodsID, "50");
				for (Map<String, String> codeMap : codeList) {
					for (Integer period : periodArray) {
						if (codeMap.get("codePeriod").equals(period.toString())) {
							if (!codeMap.get("codeRNO").equals("") || isUserQuery) {
								MyLog.outLog("补零操作正在查：", goodsID, period, codeMap.get("codeRNO"));
								GetUserBuyServer getUserBuyServer = GetUserBuyServer
										.getUserBuyServer(codeMap.get("codeID"));
								getUserBuyServer.setBarcodernoInfo(codeMap.get("codeRNO"), codeMap.get("userName"),
										codeMap.get("userWeb"));
								getUserBuyServer.GetGoodsPeriodInfo(goodsID, period.toString(), codeMap.get("codeID"),
										false);
								if (price < 200) {
									Thread.sleep(3000);
								} else if (price < 30000) {
									Thread.sleep(10 * 1000);
								} else {
									Thread.sleep(30000);
								}
							}
						}
					}
				}
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public void stopBackgroundMonitorServerThread() {
		if (backgroundMonitorServerThread != null) {
			backgroundMonitorServerThread.stop();
			backgroundMonitorServerThread = null;
		}
	}
}
